#include "DebugTask.h"
#include "DebugData.h"
#include "UartDMARcv_Proc.h"
#include "stdio.h"

UartProcInfo uart1RcvItInfo = {.cu32RecvBufSize = DEBUGBUF_SIZE};
static void StartDebugTask(void const * argument);

osMessageQId g_Uart1MsgQueue;
osMessageQDef(g_Uart1MsgQueue, 5, uint32_t);

static void DebugComRecive(uint8_t *rxBuffer, uint16_t rxBufferSize)
{
	HAL_UART_Receive_DMA(&huart1, rxBuffer, rxBufferSize);
}

void DebugTask_Init(void) 
{
	osThreadId DebugTaskHandle;
	osThreadDef(debugTask, StartDebugTask, osPriorityNormal, 0, 256);
	
	g_Uart1MsgQueue = osMessageCreate(osMessageQ(g_Uart1MsgQueue), NULL);
	
  DebugTaskHandle = osThreadCreate(osThread(debugTask), NULL);
}

static void StartDebugTask(void const * argument)
{
	uint8_t uart1Buff[DEBUGBUF_SIZE] = { 0x0 };
//	package* pUart1Pack = InitUart1Data();
	
	DebugComRecive(uart1Buff, DEBUGBUF_SIZE);
	
	osEvent evt;
	uint32_t u32DataLength = 0;
	uint8_t u8PackCnt = 0;
	uint16_t u16RemineCnt = 0;
	uint8_t i = 0;

	if(InitUartProcInfo(&uart1RcvItInfo, &huart1, 
			DEBUGBUF_SIZE, uart1Buff, g_Uart1MsgQueue))
	{
		__HAL_UART_ENABLE_IT(&huart1, UART_IT_IDLE);
	}
#ifdef DEBUG
		printf("Init Uart1 ProcInfo OK\n\r");
	}
	else
	{
		printf("Init Uart1 ProcInfo Failed!\n\r");
	}
#endif	
		
	HAL_UART_Receive_DMA(&huart1, uart1Buff, DEBUGBUF_SIZE);
	while(1)
	{
		evt = osMessageGet(g_Uart1MsgQueue, osWaitForever);
		
		if (evt.status == osEventMessage) 
		{
			u32DataLength = (uint32_t)evt.value.p;
			
			
			
#if 0
			protol_machine(uart1Buff, u32DataLength, pUart1Pack);
			
			#include <stdio.h>
			uint8_t i = 0;
			for(i = 0; i < u32DataLength; i ++)
			{
				printf("%0x ", uart1Buff[i]);
			}
			printf("length = %d\r\n", u32DataLength);
			
			CanDeviceSendData(0xA5, uart1Buff, u32DataLength);
#endif
			
		}
//			else
//			{
//				break;
//			}
	}
}

